<?php
namespace WebApper\Shortcode;

/*
 * [task_form id='TaskForm']
 *
 */
class TaskForm extends \WebApper\Shortcode {
	
    /**
     * Define shortcode properties
     *
     */
	protected $shortcode = 'task_form';
	protected $defaults = array(
		'id' => null,	// The unique ID to use for the shortcode. Default: none. Required.
	);

    /**
     * Handles the add post shortcode
     *
     * @param array $atts
     */
    public function shortcode( $atts ) {

		global $webapper, $wpdb, $current_user;
		get_currentuserinfo();

		// Get shortcode attributes
			extract( shortcode_atts( $this->defaults, $atts ) );

		// Output HTML & JS
			?>
            <form name="<?php echo $id; ?>" id="<?php echo $id; ?>" action="" method="post" class="form-horizontal" novalidate>
                <input type="hidden" name="action" value="web_apper<?php echo $this->shortcode; ?>" />
                <input type="hidden" name="web_apper_nonce" value="<?php echo wp_create_nonce( 'WebApperAwesomeness!87' ); ?>" />
                <input type="hidden" name="web_apper_task" value="add" />
                <input type="hidden" name="web_apper_id" value="<?php echo $_GET['postID'] ?>" />
                <input type="hidden" name="web_apper_task_id" value="" />
                <div id="<?php echo $id; ?>Modal" class="modal hide fade in" tabindex="-1" role="dialog" aria-labelledby="<?php echo $id; ?>ModalLabel" aria-hidden="false">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="<?php echo $id; ?>ModalLabel">New</h3>
                    </div>
                    <div class="modal-body">
                        <div id="archiveWarning" style="display: none;"><span class="label label-important pull-left">Archive lead</span><p>&nbsp;You are about to archive a task. This will mark it as complete and it will no longer appear in your taskbox. Are you sure you want to do this?</p></div>
                        <div class="control-group">
                            <label class="control-label" for="task_template"><strong>Task template:</strong></label>
                            <div class="controls">
                                <select name="task_template">
                                    <option value="custom">custom</option>
                                    <?php
                                        // Get Task template from Database
                                        global $wpdb, $webapper;
                                        $task_templates = $wpdb->get_results( "SELECT * FROM {$webapper->tables->taskTemplates}" );
                                        foreach ( $task_templates as $template ) :
                                            echo stripslashes("<option data-subject=\"$template->subject\" data-description=\"$template->description\" data-due_in=\"$template->due_in\" data-due_unit=\"$template->due_unit\" data-assign_to_user_type=\"$template->assign_to_user_type\" data-assign_to=\"$template->assign_to\" data-status=\"$template->status\">$template->name</option>");
                                        endforeach;
                                    ?>	
                                </select>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="subject"><strong>Subject:</strong></label>
                            <div class="controls">
                            	<input type="text" name="subject">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="description"><strong>Description:</strong></label>
                            <div class="controls">
								<?php
                                $settings = array(
                                    'wpautop' => false,
                                    'media_buttons' => false,
                                    'textarea_name' => 'description',
                                    'textarea_rows' => 8,
                                    'tinymce' => array (
                                        'theme_advanced_buttons1' => 'formatselect,|,bold,italic,underline,|,bullist,blockquote,|,justifyleft,justifycenter,justifyright,justifyfull,|,link,unlink,|,spellchecker,wp_fullscreen,wp_adv'
                                    )
                                );
                                wp_editor( '', 'description', $settings , array( 'quicktags' => false ) );
                                ?>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label"><strong>Due</strong></label>
                            <div class="controls">
                                <label class="radio inline"><input type="radio" name="due" value="due_in" checked> In </label>
                                <label class="radio inline"><input type="radio" name="due" value="due_on"> On</label>
                            </div>
                        </div>
                        <div class="control-group" id="due_in">
                            <label class="control-label" for="due_in"><strong>Due in:</strong></label>
                            <div class="controls">
                                <input type="number" name="due_in" class="span1" value="1">
                                <select name="due_in_unit" class="span2">
                                    <option value="hours">hours</option>
                                    <option value="days">days</option>
                                    <option value="weeks">weeks</option>
                                    <option value="months">months</option>
                                </select>
                            </div>
                        </div>
                        <div class="control-group" id="due_on">
                            <label class="control-label"><strong>Due on:</strong></label>
                            <div class="controls">
                                <input type="date" name="due_on_date" class="span2">
                                <select name="due_on_hour" class="span1">
                                    <option></option>
                                    <?php for ( $i = 1; $i <= 12; $i++ ) echo '<option value="' . $i . '">' . $i . '</option>'; ?>
                                </select>
                                <select name="due_on_ap_pm" class="span1">
                                    <option value="am">am</option>
                                    <option value="pm">pm</option>
                                </select>
                        	</div>
                        </div>
                        <?php if ( !is_page('taskbox') ) : ?>
                        <div class="control-group">
                            <label class="control-label" for="assign_to_role"><strong>Assign to:</strong></label>
                            <div class="controls">
                                <select name="assign_to" class="span2">
                                    <option value="rotate_users">rotate users</option>
                                    <?php
                                        $users = get_users( array('role' => 'staff') );
                                        foreach ( $users as $user ) :
                                            echo '<option value="' . $user->data->ID . '">' . $user->data->display_name . '</option>';
                                        endforeach;
                                    ?>
                                </select>
                            </div>
                        </div>
                        <?php endif; ?>
                        <div class="control-group">
                            <label class="control-label" for="status"><strong>Task status:</strong></label>
                            <div class="controls">
                                <select name="status">
                                    <option value="assigned">assigned</option>
                                    <option value="started">started</option>
                                    <option value="">complete</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button id="<?php echo $id; ?>_close" class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
                        <input type="submit" value="Save" name="" id="save" data-loading-text="Saving..." class="btn btn-primary"><input type="submit" value="Update" name="" id="update" data-loading-text="Updating..." class="btn btn-primary"><input type="submit" value="Archive" name="" id="archive" class="btn btn-danger btn btn-primary" data-loading-text="Deleting...">
                    </div>
                </div>
            </form>

            <script type="text/javascript">
			
				// Store Commonly manipulated DOM nodes as objects
				nForm = jQuery('#<?php echo $id; ?>');
				nFormTask = jQuery('input[name="web_apper_task"]', nForm);

				jQuery(document).ready(function($) {

					// Set fields when a Task Template is selected
					$('select[name="task_template"]').on('change', function() {
						$('[name="subject"]').val( $(':selected', this).data('subject') );
						$('[name="description"]').val( $(':selected', this).data('description') );
						$('[name="due"][value="due_in"]').prop('checked', true);
						$('div#due_on').fadeOut(400, function() {
							$('div#due_in').fadeIn(400);
						});
						$('[name="due_in"]').val( $(':selected', this).data('due_in') );
						$('[name="due_in_unit"]').val( $(':selected', this).data('due_unit') );
						$('[name="assign_to"]').val( <?php echo $current_user->ID; ?> );
						$('[name="status"]').val( $(':selected', this).data('status') );
					});
					
					// Toggle Due in/on
					$('input[name="due"]').on('change', function() {
						if ( $(this).val() == 'due_in' ) {
							$('div#due_on').fadeOut(400, function() {
								$('div#due_in').fadeIn(400);
							});
						} else {
							$('div#due_in').fadeOut(400, function() {
								$('div#due_on').fadeIn(400);
							});
						}
					});					

				});
			</script>
            <?php
	}

}

$initialize = new TaskForm(); 

?>